package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import com.zjitc.untils.RedisUtils;
import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/29 15:02
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;

  @Override
  public ServerResponse login(String username, String password) {
    int count=userDao.isNotUsername(username);
    User user=userDao.findByUsernameAndPassword(username,password);
    if(count!=1){
      return ServerResponse.createError("用户不存在");
    }
    if(user==null){
      return ServerResponse.createError("密码错误");
    }
    user.setAnswer(null);
    user.setQuestion(null);
    user.setPassword(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse register(User user) {
    int count=userDao.isNotUsername(user.getUsername());
    if(count==1){
      return ServerResponse.createError("用户已存在");
    }
    user.setRole(0);
    user.setCreateTime(new Date());
    user.setUpdateTime(new Date());
    int count1= userDao.register(user);
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> check_valid(String str) {
    int count = userDao.isNotUsername(str);
    if (count == 1) {
      return ServerResponse.createError("用户已存在");
    }
    int count2 = userDao.checkEmail(str);
    if (count2 == 1) {
      return ServerResponse.createError("用户已存在");
    }
    return ServerResponse.createSuccess("校验成功");
  }

  @Override
  public ServerResponse<User> get_user_info(String username) {
    User user = userDao.findByUserName(username);
    if(user == null){
      return ServerResponse.createError("找不到当前用户");
    }
    user.setPassword(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> forget_get_question(String username) {
    int count = userDao.isNotUsername(username);
    if (count== 1) {
      String question= userDao.userQuestion(username);
      if (question!=null){
        User user = userDao.findByUserName(username);
        return ServerResponse.createSuccess(user.getQuestion());
      }
      return ServerResponse.createError("该用户未设置找回密码问题");
    }
    return ServerResponse.createError("此用户未注册");
  }

  @Override
  public ServerResponse<User> reset_password(String passwordOld, String passwordNew, User user) {
    int count = userDao.checkPassword(passwordOld);
    if (count == 0){
      return ServerResponse.createError("旧密码错误");
    }
    int count2 = userDao.updatePassword(user.getUsername(),passwordNew);
    if(count2 > 0){
      return ServerResponse.createSuccess("密码更新成功");
    }
    return ServerResponse.createError("密码更新失败");
  }

  @Override
  public ServerResponse<User> update_information(String email, String question, String answer, String phone, User user) {
    int count = userDao.updateInformation(user.getUsername(),email,question,answer,phone);
    if (count > 0){
      return ServerResponse.createSuccess("个人信息更新成功");
    }
    return ServerResponse.createError("个人信息更新失败");
    }

  @Override
  public ServerResponse<User> get_information(String username) {
    User user = userDao.findByUserName(username);
    if(user == null){
      return ServerResponse.createError("找不到此用户");
    }
    user.setPassword(null);
    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<User> checkQuestionAndAnswer(String username, String question, String answer) {
    int count = userDao.checkQuestion(username,question);
    if (count != 0){
      int count1 = userDao.checkAnswer(username,answer);
      if (count1 != 0){
        String token = String.valueOf(UUID.randomUUID());
        RedisUtils.add(username,token);
        return ServerResponse.createSuccess(token);
      }
    }
    return ServerResponse.createError("问题答案错误");
  }

  @Override
  public ServerResponse<User> forgetResetPassword(String username, String passwordNew, String forgetToken) {
    String token = RedisUtils.find(username);
    if (token.equals(forgetToken)){
      int count1 = userDao.updatePassword(username,passwordNew);
      if (count1>0){
        RedisUtils.delete(username);
        return ServerResponse.createSuccess("修改密码成功");
      }
    }
    return ServerResponse.createError("修改密码失败");
  }


}

